var express = require('express');
var router = express.Router();
var db = require('../../data');

/* GET home page. */
router.get('/', function(req, res, next) {
    var session = req.session;
    var data = db.model;


    if(!session.user || !session.user.login){
        res.redirect('/admin/login');
    }else{

        data.basicFacts.findOne(function(err, docs){
            if(err)
                return next();

            var returnData = {
                path: 'odds',
                basicFacts : docs,
                user : session.user
            };

            data.odds.findOne(function(err, docs){
                if(err)
                    return next();

                returnData.data = docs;

                res.render('admin/odds', returnData);

            });

        });

    };
});

router.post('/save', function(req, res, next){
    var data = db.model;
    console.log(req.body);
    data.odds.update({}, req.body, function(err, docs){
        if(err){
            console.log('err:',err);
            return next();
        };
        res.redirect('/admin/odds');
    });
});

module.exports = router;
